package com.yanxu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yanxu.config.DBUserDetailManager;
import com.yanxu.entity.User;
import com.yanxu.mapper.UserMapper;
import com.yanxu.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.UserDetailsManager;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2025-10-10-2025
 * @Description: 用户 service
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Resource
    private UserDetailsManager userDetailsManager;

    @Override
    public List<User> getUserList() {
        return userMapper.selectList(new QueryWrapper<>());
    }

    @Override
    public User selectByUserName(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>().eq("username", username);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public void saveUser(User user) {
        UserDetails userDetails = org.springframework.security.core.userdetails.User
                .withDefaultPasswordEncoder()
                .username(user.getUsername())
                .password(user.getPassword())
                //.roles("")
                .build();
        //需要使用我们自定义的 用户管理器 进行处理
        userDetailsManager.createUser(userDetails);
    }
}